import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.justtest.FirstServiceGrpc;
import io.grpc.justtest.TestRequest;
import io.grpc.justtest.TestResponse;

import java.util.concurrent.TimeUnit;

/**
 * <p>Description:客户端</p >
 * <p>Copyright: Copyright (c)2021</p >
 * <P>Created Date :2021-01-28</P>
 * <P>@version 1.0</P>
 */
public class TestClient {

    private final ManagedChannel channel;
    private final FirstServiceGrpc.FirstServiceBlockingStub blockingStub;
    private static final String host = "127.0.0.1";
    private static final int ip = 9527;

    public TestClient(String host, int port) {
        // usePlaintext表示明文传输，否则需要配置ssl
        // channel  表示通信通道
        channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
        // 存根，创建阻塞式的stub
        blockingStub = FirstServiceGrpc.newBlockingStub(channel);
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    public void testResult(Integer num1, Integer num2) {
        TestRequest request = TestRequest.newBuilder().setNum1(num1).setNum2(num2).build();
        TestResponse response = blockingStub.testgRPC(request);
        System.out.println(response.getRes());
    }

    public static void main(String[] args) {
        TestClient client = new TestClient(host, ip);
        client.testResult(2, 1);
    }
}
